﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using MeasureAbstract.Base;
using MeasureAbstract.IMotionDevice;
using MeasureAbstract.ISignalGenerator;
using MeasureAbstract.ISilentAreaMeasure;
using MeasureAbstract.IVectorNetworkAnalyzer;
using MeasureLibrary.SilentAreaMeasure;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 紧缩场反射面装调测试系统.ViewModels
{
    public partial class InstrumentSetupViewModel : ObservableObject
    {
        private ISilentAreaMeasure? _Measure;
        public ISilentAreaMeasure? Measure
        {
            get { return _Measure; }
            set { SetProperty(ref _Measure, value); }
        }

        [ObservableProperty]
        private ObservableCollection<string> motionDeviceList = new ObservableCollection<string>();

        public string SelectedMotionDevice
        {
            get { return _SelectedMotionDevice; }
            set
            {
                SetProperty(ref _SelectedMotionDevice, value);
                SystemManage.SystemConfig.SelectedMotionDevice = value;
                SystemManage.Measure.DeviceList[0] = SystemManage.MotionDeviceDict[value];
                SystemManage.SystemConfig.MotionDeviceConfig = (SystemManage.Measure.DeviceList[0] as IMotionDevice).Config;
            }
        }
        private string _SelectedMotionDevice = "";

        [ObservableProperty]
        private ObservableCollection<string> vectorNetworkAnalyzerList = new ObservableCollection<string>();

        public string SelectedVectorNetworkAnalyzer
        {
            get { return _SelectedVectorNetworkAnalyzer; }
            set
            {
                SetProperty(ref _SelectedVectorNetworkAnalyzer, value);
                SystemManage.SystemConfig.SelectedVectorNetworkAnalyzer = value;
                SystemManage.Measure.DeviceList[1] = SystemManage.VectorNetworkAnalyzerDict[value];
                SystemManage.SystemConfig.VectorNetworkAnalyzerConfig = (SystemManage.Measure.DeviceList[1] as IVectorNetworkAnalyzer).Config;
            }
        }
        private string _SelectedVectorNetworkAnalyzer = "";


        [ObservableProperty]
        private ObservableCollection<string> signalGeneratorList = new ObservableCollection<string>();

        public string SelectedSignalGenerator
        {
            get { return _SelectedSignalGenerator; }
            set
            {
                SetProperty(ref _SelectedSignalGenerator, value);
                SystemManage.SystemConfig.SelectedSignalGenerator = value;
                if (SystemManage.Measure.DeviceList.Count < 3) return;
                SystemManage.Measure.DeviceList[2] = SystemManage.SignalGeneratorDict[value];
                SystemManage.SystemConfig.SignalGeneratorConfig = (SystemManage.Measure.DeviceList[2] as ISignalGenerator).Config;
            }
        }
        private string _SelectedSignalGenerator = "";



        public InstrumentSetupViewModel()
        {

        }

        [RelayCommand]
        void MotionDeviceConnect()
        {
            Measure.DeviceList[0].Connect();
        }
        [RelayCommand]
        void MotionDeviceDisConnect()
        {
            Measure.DeviceList[0].DisConnect();
        }

        [RelayCommand]
        void VectorNetworkAnalyzerConnect()
        {
            Measure.DeviceList[1].Connect();
        }
        [RelayCommand]
        void VectorNetworkAnalyzerDisConnect()
        {
            Measure.DeviceList[1].DisConnect();
        }

        [RelayCommand]
        void SignalGeneratorConnect()
        {
            Measure.DeviceList[2].Connect();
        }
        [RelayCommand]
        void SignalGeneratorDisConnect()
        {
            Measure.DeviceList[2].DisConnect();
        }

        [RelayCommand]
        public void OutPut(object Obj)
        {
            if (Obj == null) throw new ArgumentNullException();
            if (Obj.ToString().ToUpper() == "ON")
            {
                Growl.Info("开射频");
                //Measure.VectorNetworkAnalyzer.OutPut(true);
            }
            else if (Obj.ToString().ToUpper() == "OFF")
            {
                Growl.Info("关射频");

                //Measure.VectorNetworkAnalyzer.OutPut(false);
            }
            else
            {

            }
        }

        [RelayCommand]
        public void Run(object obj)
        {
            string AxisName = obj.ToString();
            IMotionDevice MotionDevice = Measure.DeviceList[0] as IMotionDevice;
            if (MotionDevice != null)
            {
                foreach (var temp in MotionDevice.Config.Axes)
                {
                    if (AxisName == temp.AxisName)
                    {
                        MotionDevice.Run(temp.AxisID, temp.SetStartPosition, temp.SetSpeed, 0, 0);
                    }
                }
            }


        }
        [RelayCommand]
        public void Stop(object obj)
        {
            string AxisName = obj.ToString();
            IMotionDevice MotionDevice = Measure.DeviceList[0] as IMotionDevice;
            if (MotionDevice != null)
            {
                foreach (var temp in MotionDevice.Config.Axes)
                {
                    if (AxisName == temp.AxisName)
                    {
                        MotionDevice.Stop(temp.AxisID);
                    }
                }
            }


        }

        [RelayCommand]
        public void TriggerOpen(object obj)
        {
            string AxisName = obj.ToString();
            IMotionDevice MotionDevice = Measure.DeviceList[0] as IMotionDevice;
            if (MotionDevice != null)
            {
                foreach (var temp in MotionDevice.Config.Axes)
                {
                    if (AxisName == temp.AxisName)
                    {
                        MotionDevice.TriggerOpen(temp.AxisID, temp.PulseTriggerStartPosition, temp.PulseTriggerEndPosition, temp.PulseTriggerInterval, 10);
                    }
                }
            }


        }

        [RelayCommand]
        public void TriggerOne(object obj)
        {
            string AxisName = obj.ToString();
            IMotionDevice MotionDevice = Measure.DeviceList[0] as IMotionDevice;
            if (MotionDevice != null)
            {
                foreach (var temp in MotionDevice.Config.Axes)
                {
                    if (AxisName == temp.AxisName)
                    {
                        MotionDevice.TriggerOne(temp.AxisID);
                    }
                }
            }


        }
        [RelayCommand]
        public void TriggerClose(object obj)
        {
            string AxisName = obj.ToString();
            IMotionDevice MotionDevice = Measure.DeviceList[0] as IMotionDevice;
            if (MotionDevice != null)
            {
                foreach (var temp in MotionDevice.Config.Axes)
                {
                    if (AxisName == temp.AxisName)
                    {
                        MotionDevice.TriggerClose(temp.AxisID);
                    }
                }
            }


        }

        [RelayCommand]
        async Task Init()
        {
            if(Measure.Flag.InitializingFlag)
            {
                HandyControl.Controls.Growl.Info("正在初始化,请稍后!");
                return;
            }
            Measure.Flag.InitializingFlag = true;
            await Measure.MeasureInitialize(null);
            HandyControl.Controls.Growl.Info("初始化完成!");
            Measure.Flag.InitializingFlag = false;

        }
    }
}
